<?php

namespace app\index\model;

use think\Model;

class Group extends Model
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';
    protected $updateTime = false;

    /**
     * 返回已选择的 group
     */
    public static function getSelected()
    {
        $groups = self::where(array('selected' => 1))->select();
        $ids = [];
        foreach ($groups as $group) {
            array_push($ids, $group['id']);
        }

        return $ids;
    }

    public static function setSelected($ids)
    {
        self::where('id > 0')->setField('selected', 0);
        self::whereIn('id', $ids)->setField('selected', 1);
    }
}
